package com.loan.web.api;

import com.loan.entity.ResponseResult;
import com.loan.exception.*;
import com.mysql.cj.jdbc.exceptions.MysqlDataTruncation;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.ResponseUtil;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class ExceptionListerHandler {
    /*
     * 功能描述: <br>
     * 处理有效性校验异常
     * @Param: [ex]
     * @Return: com.loan.entity.ResponseResult<java.lang.Void>
     * @Author: wYin
     * @Date: 2022/1/18 21:35
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult<Map<String,String>> exceptionHandler(MethodArgumentNotValidException ex){
        Map<String,String> map = new HashMap<>();
        //得到方法中参数异常的信息
        ex.getFieldErrors().forEach(e->{
            String fieldName = e.getField();
            String fieldMsg = e.getDefaultMessage();
            map.put(fieldName,fieldMsg);
        });
        return new ResponseResult<>(500,"方法参数无效",map);
    }

    /**
     * 功能描述: <br>
     * 处理申请表信息中异常
     * @Param: [ex]
     * @Return: com.loan.entity.ResponseResult<java.lang.Void>
     * @Author: wYin
     * @Date: 2022/1/18 21:34
     */
    @ExceptionHandler(ApplicationException.class)
    public ResponseResult<Void> applicationException(ApplicationException ex){
        return new ResponseResult<>(501,ex.getMessage());
    }

    /**
     * 功能描述: <br>
     * 处理贷款客户中异常
     * @Param: [ex]
     * @Return: com.loan.entity.ResponseResult<java.lang.Void>
     * @Author: wYin
     * @Date: 2022/1/18 21:36
     */
    @ExceptionHandler(CustomerException.class)
    public ResponseResult<Void> customerException(CustomerException ex){
        return new ResponseResult<>(502,ex.getMessage());
    }

    /**
     * 功能描述: <br>
     * 产品表中的中异常处理
     * @Param: [ex]
     * @Return: com.loan.entity.ResponseResult<java.lang.Void>
     * @Author: wYin
     * @Date: 2022/1/18 21:38
     */
    @ExceptionHandler(ProductException.class)
    public ResponseResult<Void> productException(ProductException ex){
        return new ResponseResult<>(503,ex.getMessage());
    }

    /**
     * 功能描述: <br>
     * 合同表中的异常处理
     * @Param: [ex]
     * @Return: com.loan.entity.ResponseResult<java.lang.Void>
     * @Author: wYin
     * @Date: 2022/1/18 21:41
     */
    @ExceptionHandler(ContractException.class)
    public ResponseResult<Void> contractException(ContractException ex){
        return new ResponseResult<>(504,ex.getMessage());
    }

    /**
     * 功能描述: <br>
     * 员工表中异常处理
     * @Param: [ex]
     * @Return: com.loan.entity.ResponseResult<java.lang.Void>
     * @Author: wYin
     * @Date: 2022/1/18 21:43
     */
    @ExceptionHandler(EmpException.class)
    public ResponseResult<Void> empException(EmpException ex){
        return new ResponseResult<>(505,ex.getMessage());
    }

    /**
     * 功能描述: <br>
     * 操作表中的异常处理
     * @Param: [ex]
     * @Return: com.loan.entity.ResponseResult<java.lang.Void>
     * @Author: wYin
     * @Date: 2022/1/18 21:44
     */
    @ExceptionHandler(HandleException.class)
    public ResponseResult<Void> handleException(HandleException ex){
        return new ResponseResult<>(506,ex.getMessage());
    }

    /**
     * 功能描述: <br>
     * 还款表中数据异常处理
     * @Param: [ex]
     * @Return: com.loan.entity.ResponseResult<java.lang.Void>
     * @Author: wYin
     * @Date: 2022/1/18 21:46
     */
    @ExceptionHandler(RepaymentException.class)
    public ResponseResult<Void> repaymentException(RepaymentException ex){
        return new ResponseResult<>(507,ex.getMessage());
    }

    /**
     * @Desc: 用户表中的异常处理
     * @Param: [ex]
     * @Return: com.loan.entity.ResponseResult<java.lang.Void>
     * @Author: wYin
     * @Date: 2022/1/19 21:24
     */
    @ExceptionHandler(UserException.class)
    public ResponseResult<Void> userException(UserException ex){
        return new ResponseResult<>(508,ex.getMessage());
    }

    /**
     * @Desc: 还款方式表中的异常处理
     * @Param: [ex]
     * @Return: com.loan.entity.ResponseResult<java.lang.Void>
     * @Author: wYin
     * @Date: 2022/1/19 21:24
     */
    @ExceptionHandler(RepaymentMethodException.class)
    public ResponseResult<Void> repaymentMethodException(RepaymentMethodException ex){
        return new ResponseResult<>(509,ex.getMessage());
    }

    /**
     * @Desc: 超出规定范围-精度
     * @Param: []
     * @Return: com.loan.entity.ResponseResult<java.lang.Void>
     * @Author: wYin
     * @Date: 2022/1/23 18:30
     */
    @ExceptionHandler(MysqlDataTruncation.class)
    public ResponseResult<Void> mySqlDateTruncationException(){
        return new ResponseResult<>(600,"贷款金额超过最大贷款范围");
    }

    @ExceptionHandler(CannotGetJdbcConnectionException.class)
    public ResponseResult<Void> cannotGetJDBC(){
        return new ResponseResult<>(601,"网络错误，连接异常");
    }

}
